﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EarthProject
{
    public partial class FormCountryInformation : Form
    {
        private EarthDataContextDataContext dataContext = null;
        private BindingSource bindgingSourceLanguages;
        private BindingSource bindgingSourceCountries;

        public FormCountryInformation()
        {
            InitializeComponent();
        }

        private void FormCountryInformation_Load(object sender, EventArgs e)
        {
            AttachGlobalLog();
            LoadContinents();
        }

        private void AttachGlobalLog()
        {
            EarthDataContextDataContext.GlobalLog = new TextBoxTextWriter(this.textBoxLog);
        }

        private void LoadCountries(int continentId)
        {
            this.dataContext = new EarthDataContextDataContext();
            
            var countries =
                from c in this.dataContext.Countries
                where c.ContinentID == continentId
                select c.CountryName;
            var languages =
                from c in this.dataContext.Countries
                where c.ContinentID == continentId
                select c.Language;

            bindgingSourceCountries = new BindingSource(countries, "");
            bindgingSourceLanguages = new BindingSource(languages, "");
            textBoxCountryLanguage.DataBindings.Clear();
            textBoxCountryName.DataBindings.Clear();
            textBoxCountryName.DataBindings.Add("Text", bindgingSourceCountries, "");
            textBoxCountryLanguage.DataBindings.Add("Text", bindgingSourceLanguages, "");
        }

        private void MovePrevious(BindingSource bindingSource)
        {
            bindingSource.MovePrevious();
        }

        private void MoveNext(BindingSource bindingSource)
        {
            bindingSource.MoveNext();
        }

        private void MoveFirst(BindingSource bindingSource)
        {
            bindingSource.MoveFirst();
        }

        private void MoveLast(BindingSource bindingSource)
        {
            bindingSource.MoveLast();
        }

        private void buttonMovePrevious_Click(object sender, EventArgs e)
        {
            MovePrevious(bindgingSourceCountries);
            MovePrevious(bindgingSourceLanguages);

        }

        private void buttonMoveNext_Click(object sender, EventArgs e)
        {
            MoveNext(bindgingSourceLanguages);
            MoveNext(bindgingSourceCountries);
        }

        private void buttonMoveFirst_Click(object sender, EventArgs e)
        {
            MoveFirst(bindgingSourceCountries);
            MoveFirst(bindgingSourceLanguages);
        }

        private void buttonMoveLast_Click(object sender, EventArgs e)
        {
            MoveLast(bindgingSourceLanguages);
            MoveLast(bindgingSourceCountries);
        }

        private void LoadTowns(string countryName)
        {
            this.dataContext = new EarthDataContextDataContext();
            this.townBindingSource.DataSource =
                from t in this.dataContext.Towns
                where t.Country.CountryName == countryName
                select t;
        }

        private void textBoxCountryName_TextChanged(object sender, EventArgs e)
        {
            LoadTowns(this.textBoxCountryName.Text);
        }

        private void LoadContinents()
        {
            this.dataContext = new EarthDataContextDataContext();
            var continents =
                from c in this.dataContext.Continents
                select c;
            listBoxContinents.DataSource = continents;
            listBoxContinents.DisplayMember = "ContinentName";
        }

        private void listBoxContinents_Click(object sender, EventArgs e)
        {
            Continent continent = (Continent)listBoxContinents.SelectedItem;
            LoadCountries(continent.ContinentID);
        }
    }
}
